

local item_class = require 'types.item'.__index

local tags = {'S+','S_','A+','A_','B+','B_','C+','C_','D+','D_'}
--注册
local excel = {}
for _,data in ipairs(load_excel('符文装备.xlsx','洗练数值')) do
    local name = data.name
    if name then
        excel[#excel + 1] = data
    end
end


function item_class:copy_info()
    return {
        owner_player = self.owner_player,
        _xl_state = self._xl_state,
        _xl_tips = self._xl_tips,
    }
end

item_class.xl_gold = 10000
function item_class:xl_tip()
    local tip = self._xl_tips or ''
    if self['无法洗练'] or self.item_level<self.max_level then
        return ('|n=================|n随机词条|n%s|n|n'):format(tip)
    end
    return ('|n=================|n点击洗练|cffffff00%s|r金币|n%s|n|n'):format(self.xl_gold,tip)
    --return ('|n=================|n随机词条|n%s|n|n'):format(tip)
end

--[洗练文本介绍]
item_class._xl_tips = ''
function item_class:update_xl()
    local tips = {}
    local state = self._xl_state or {}
    table.sort(state,function(a,b)
        return a.level<b.level
    end)
    for a,da in ipairs(state) do
        tips[a] = da.tip
    end
    self._xl_tips = table.concat(tips,'|n')
    return tips
end


function item_class:ex_tip()
    local player = self.owner_player or ac.player.self
    local tip = ('|cff999999所属玩家：|r%s'):format(player:get_name())
    return tip
end

function item_class:random_entry(lock)
    local lock = lock or {}
    local entry = {}
    local max = #tags
    local index = math.ceil(math.sqrt(math.random(1,max^2)))
    local tag = tags[index]
    for _,data in ipairs(excel) do
        local key = data.name
        local num = data[tag]
        if lock[key]==nil and num then
            local tip = ('%s%s'):format(tag,sx_tip(key,num))
            entry[#entry + 1] = {key,num,tip = tip,level = index}
        end
    end
    local data = table.random(entry)
    return data
end

function item_class:reset_entry(lock)
    local num = self.entry_count
    local max = #tags
    local state = {}
    for a=1,num do
        local data = self:random_entry()
        state[a] = data
    end

    load_item_state(hero,self._xl_state,-1)
    load_item_state(hero,state,1)
    self._xl_state = state
    self:update_xl()
    
end


--[词条洗练]
ac.game:event '单位-点击物品'(function(_,hero,item)
    local player = hero.owner
    local level = item.item_level
    if item.item_class=='符文装备' and item.on_use==nil then
        if level<item.max_level then
            if player:get('金币')<item.gold then
                show_item_tip(player,item,'金币')
                return
            end
            player:add('金币',-item.gold)
            item:set_item_level(level + 1)
            if level>item.entry_count and item['词条稳定']==nil then
                item:reset_entry()
            end
            show_item_tip(player,item,'刷新')
            return
        end
        if item['无法洗练']==nil and level==item.max_level then
            if player:get('金币')<item.xl_gold then
                return
            end
            player:add('金币',-item.xl_gold)
            item:reset_entry()
            show_item_tip(player,item,'刷新')
        end
    end
end)



ac.game:event '单位-添加物品属性'(function(_,hero,item)
    load_item_state(hero,item._xl_state,1)
end)

ac.game:event '单位-移除物品属性'(function(_,hero,item)
    load_item_state(hero,item._xl_state,-1)
end)